package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.ScoreConvertDao;
import com.qdlc.p2p.dal.dto.BondTender;
import com.qdlc.p2p.dal.dto.Goods;
import com.qdlc.p2p.dal.dto.ScoreConvert;
import com.qdlc.p2p.dal.model.ScoreConvertModel;

@Repository("scoreConvertDao")
public class ScoreConvertDaoImpl extends BaseDaoImpl<ScoreConvert> implements ScoreConvertDao {
	
	@Override
	public PageDataList<ScoreConvertModel> findPageList(ScoreConvertModel model) {
		QueryParam param = QueryParam.getInstance();
		if (StringUtil.isNotBlank(model.getSearchName())) {
			SearchFilter orFilter1 = new SearchFilter("user.userName", Operators.LIKE, model.getSearchName().trim());
			SearchFilter orFilter2 = new SearchFilter("goods.name", Operators.LIKE, model.getSearchName().trim());
			param.addOrFilter(orFilter1, orFilter2);
		} else {
			if (model.getUser() != null && model.getUser().getUserId() > 0) {
				param.addParam("user.userId", model.getUser().getUserId());
			}
			if (StringUtil.isNotBlank(model.getName())) {
				param.addParam("goods.name", Operators.LIKE, model.getName().trim());
			}
			if (StringUtil.isNotBlank(model.getUserName())) {
				param.addParam("user.userName", Operators.LIKE, model.getUserName().trim());
			}
			if(StringUtil.isNotBlank(model.getReceiveMobile())){
				param.addParam("receiveMobile", Operators.LIKE, model.getReceiveMobile().trim());
			}
			if (model.getStatus() != 99) {
				param.addParam("status", model.getStatus());
			}
			if (StringUtil.isNotBlank(model.getStartTime())) {
				Date start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
				param.addParam("addTime", Operators.GTE, start);
			}
			if (StringUtil.isNotBlank(model.getEndTime())) {
				Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
				param.addParam("addTime", Operators.LTE, end);
			}
			
		}
		param.addOrder(OrderType.DESC, "id");
		param.addPage(model.getPage(), model.getRows());
		PageDataList<ScoreConvert> itemPage = super.findPageList(param);
		List<ScoreConvertModel> list = new ArrayList<ScoreConvertModel>();
		PageDataList<ScoreConvertModel> modelPage = new PageDataList<ScoreConvertModel>();
		if (itemPage != null && itemPage.getList() != null && itemPage.getList().size() > 0) {
			for (int i = 0; i < itemPage.getList().size(); i++) {
				ScoreConvert item = itemPage.getList().get(i);
				ScoreConvertModel model_ = ScoreConvertModel.instance(item);
				Goods goods = item.getGoods();
				// GoodsPic pic = goodsPicDao.getGoodsFirstPic(goods.getId());
				model_.setImgSrc(goods.getPicPath());
				model_.setName(goods.getName());
				model_.setUserName(item.getUser().getUserName());
				model_.setGoodsId(goods.getId());
				model_.setIsVirtual(goods.getIsVirtual());
				list.add(model_);
			}
		}
		modelPage.setList(list);
		modelPage.setPage(itemPage.getPage());
		return modelPage;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ScoreConvert> getNewScoreConvertList() {
		Query query = em.createNativeQuery("SELECT * FROM " + Global.DB_PREFIX + "bond_tender ORDER BY add_time DESC LIMIT 10",BondTender.class);
		List<ScoreConvert> list =(List<ScoreConvert>)query.getResultList();
		return list;
	}

}
